//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Interpolation2D.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Math/Interpolations/Extrapolator.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D () : CExtrapolator(CInterpolation2D::typeid)
{
    try
    {
#ifdef HANDLE
        _phInterpolation2D = NULL;
#endif
        _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (new QuantLib::Interpolation2D ( ));
        SetExtrapolator (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (boost::shared_ptr<QuantLib::Interpolation2D>& childNative, Object^ owner) : CExtrapolator(CInterpolation2D::typeid)
{
#ifdef HANDLE
	_phInterpolation2D = NULL;
#endif
	_ppInterpolation2D = &childNative;
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
}
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (QuantLib::Interpolation2D& childNative, Object^ owner) : CExtrapolator(CInterpolation2D::typeid)
{
#ifdef HANDLE
	_phInterpolation2D = NULL;
#endif
	_ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (&childNative);
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
    _Interpolation2DOwner = owner;
    _ExtrapolatorOwner = owner;
}

Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (CInterpolation2D^ copy) : CExtrapolator(CInterpolation2D::typeid)
{
#ifdef HANDLE
	_phInterpolation2D = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (copy->GetShared());
        _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
    }
}
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (System::Type^ t) : CExtrapolator(CInterpolation2D::typeid)
{
#ifdef HANDLE
	_phInterpolation2D = NULL;
#endif
	if (!t->IsSubclassOf(CInterpolation2D::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (QuantLib::Handle<QuantLib::Interpolation2D>& childNative, Object^ owner)  : CExtrapolator(CInterpolation2D::typeid)
{
	_phInterpolation2D = &childNative;
	_ppInterpolation2D = &static_cast<boost::shared_ptr<QuantLib::Interpolation2D>>(childNative.currentLink());
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
    _Interpolation2DOwner = owner;
}
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (QuantLib::Handle<QuantLib::Interpolation2D> childNative)  : CExtrapolator(CInterpolation2D::typeid)
{
	_phInterpolation2D = &childNative;
	_ppInterpolation2D = &static_cast<boost::shared_ptr<QuantLib::Interpolation2D>>(childNative.currentLink());
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CInterpolation2D::CInterpolation2D (QuantLib::Interpolation2D childNative)  : CExtrapolator(CInterpolation2D::typeid)
{
#ifdef HANDLE
	_phInterpolation2D = NULL;
#endif
	_ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (new QuantLib::Interpolation2D (childNative));
    _ppExtrapolator = new boost::shared_ptr<QuantLib::Extrapolator> (boost::dynamic_pointer_cast<QuantLib::Extrapolator> (*_ppInterpolation2D));
}
#endif

Cephei::QL::Math::Interpolations::CInterpolation2D::~CInterpolation2D ()
{
    if (_ppInterpolation2D != NULL)
    {
	    delete _ppInterpolation2D;
        _ppInterpolation2D = NULL;
    }
}
Cephei::QL::Math::Interpolations::CInterpolation2D::!CInterpolation2D ()
{
    if (_ppInterpolation2D != NULL)
    {
	    delete _ppInterpolation2D;
    }
}
QuantLib::Interpolation2D& Cephei::QL::Math::Interpolations::CInterpolation2D::GetReference ()
{
    if (_ppInterpolation2D == NULL) throw gcnew NativeNullException ();
	return **_ppInterpolation2D;
}
boost::shared_ptr<QuantLib::Interpolation2D>& Cephei::QL::Math::Interpolations::CInterpolation2D::GetShared ()
{
    if (_ppInterpolation2D == NULL) throw gcnew NativeNullException ();
	return *_ppInterpolation2D;
}
QuantLib::Interpolation2D* Cephei::QL::Math::Interpolations::CInterpolation2D::GetPointer ()
{
    if (_ppInterpolation2D == NULL) throw gcnew NativeNullException ();
	return &**_ppInterpolation2D;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Interpolation2D>& Cephei::QL::Math::Interpolations::CInterpolation2D::GetHandle ()
{
	if (_phInterpolation2D == NULL)
	{
		_phInterpolation2D = new Handle<QuantLib::Interpolation2D> (*_ppInterpolation2D);
	}
	return *_phInterpolation2D;
}
#endif
bool Cephei::QL::Math::Interpolations::CInterpolation2D::HasNative () 
{
	return (_ppInterpolation2D != NULL);
}

Boolean Cephei::QL::Math::Interpolations::CInterpolation2D::IsInRange (Double x, Double y)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	bool _rv = (bool)(*_ppInterpolation2D)->isInRange ( _x,  _y );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Interpolations::CInterpolation2D::LocateX (Double x)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppInterpolation2D)->locateX ( _x );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Interpolations::CInterpolation2D::LocateY (Double y)
{
    try
    {
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppInterpolation2D)->locateY ( _y );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::IInterpolation2D^ Cephei::QL::Math::Interpolations::CInterpolation2D::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppInterpolation2D)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CInterpolation2D::XMax::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation2D)->xMax ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CInterpolation2D::XMin::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation2D)->xMin ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Math::Interpolations::CInterpolation2D::XValues::get ()
{
    try
    {
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppInterpolation2D)->xValues ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CInterpolation2D::YMax::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation2D)->yMax ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Interpolations::CInterpolation2D::YMin::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterpolation2D)->yMin ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Math::Interpolations::CInterpolation2D::YValues::get ()
{
    try
    {
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppInterpolation2D)->yValues ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Math::Interpolations::CInterpolation2D::ZData::get ()
{
    try
    {
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppInterpolation2D)->zData ( );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::IInterpolation2D^ Cephei::QL::Math::Interpolations::CInterpolation2D_Factory::Create ()
{
    return gcnew CInterpolation2D ();
}
